package com.ruoyi.fangyuan.config;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Title: DBContextHolder
 * @Description:
 * @Auther: 龙天
 * @Version: 1.0
 * @create 2023/5/5 15:06
 */
public class DBContextHolder {
    /**
     * 确保创建的变量只被同一个线程进行读写操作
     */
    private static final ThreadLocal<DBTypeEnum> CONTEXT_HOLDER = new ThreadLocal<>();

    /**
     * 原子操作类，确保线程安全
     */
    private static  final AtomicInteger COUNTER = new AtomicInteger(0);

    /**
     * 设置当前线程的枚举值
     * @param dbTypeEnum
     */
    public static void set (DBTypeEnum dbTypeEnum) {
        CONTEXT_HOLDER.set(dbTypeEnum);
    }

    /**
     * 获取当前线程的枚举值
     * @return
     */
    public static DBTypeEnum get () {
        return CONTEXT_HOLDER.get();
    }

    /**
     * 移除当前线程的枚举值
     */
    public static void remove () {
        CONTEXT_HOLDER.remove();
    }

    /**
     * 切换到主数据库
     */
    public static void master () {
        set(DBTypeEnum.MASTER);
        System.out.println("切换到" + DBTypeEnum.MASTER);
    }

    /**
     * 切换到从数据库
     */
    public static void slave () {
        // 轮询，枚举值数量减去一个主库，表示从库数量
        int index = COUNTER.getAndIncrement() % (DBTypeEnum.values().length - 1);
        if (COUNTER.get() > 9999) {
            COUNTER.set(0);
        }
        set(DBTypeEnum.values()[index + 1]);
        System.out.println("切换到" + DBTypeEnum.values()[index + 1]);
    }
}
